<template>
  <div>
    <div style="margin: 16px 0 0 30px ">
      <div style="width: 150px;float: left;">
        <el-input
          placeholder="请输入内容"
          v-model="input"
          clearable>
        </el-input>
      </div>
      <el-button type="primary" @click="search(input)">搜索</el-button>
      <el-button type="success" plain @click="refresh">刷新</el-button>
      <el-button  type="success" round @click="dialogFormVisiblePostGoodsTC()" >添加</el-button>
    </div>

  <div class="app-container">
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="ID" width="95">
        <template slot-scope="scope">
          {{ scope.row.goodsId }}
        </template>
      </el-table-column>
      <el-table-column label="商品名字">
        <template slot-scope="scope">
          {{ scope.row.goodsName }}
        </template>
      </el-table-column>
      <el-table-column label="图片" width="180" align="center">
        <template slot-scope="scope">
          <img :src="scope.row.goodsImg" width="120px">
        </template>
      </el-table-column>
      <el-table-column label="价格" width="110" align="center">
        <template slot-scope="scope">
          {{ scope.row.goodsPrice }}
        </template>
      </el-table-column>
      <el-table-column label="描述"  align="center">
        <template slot-scope="scope">
          {{ scope.row.goodsDescribe }}
        </template>
      </el-table-column>
      <el-table-column label="月销售" width="110" align="center">
        <template slot-scope="scope">
          {{ scope.row.monthlySales }}
        </template>
      </el-table-column>
      <el-table-column label="总销售" width="110" align="center">
        <template slot-scope="scope">
          {{ scope.row.totalSales }}
        </template>
      </el-table-column>
      <el-table-column class-name="status-col" label="详细" width="110" align="center">
        <template slot-scope="scope">

          <el-tag @click="detailed(scope.row.goodsId)">详细</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="状态：绿是在售，红为下架"  align="center">
        <template slot-scope="scope" >
          <div @click="goodsStatezt(scope.$index)">
          <el-switch
            v-model="scope.row.goodsState == 1?true:false"
            active-color="#13ce66"
            inactive-color="#ff4949">
          </el-switch>
<!--          {{ scope.row.goodsState }}-->
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="260"  align="center">
        <template slot-scope="scope">
<!--          <el-button  type="success" round @click="dialogFormVisiblePostGoods = true" >添加</el-button>-->
          <el-button type="primary" round @click="dialogFormVisiblePutGoodsId(scope.$index)">修改</el-button>
          <el-button type="danger" icon="el-icon-delete" circle @click="goodsDel(scope.row.goodsId)"> </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @current-change="handleCurrentChange"
      :page-size="5"
      :current-page.sync="currentPage"
      background
      layout="prev, pager, next, jumper"
      :total="count">
    </el-pagination>

    <!--添加商品弹窗-->
    <el-dialog title="添加类型"  :visible.sync="dialogFormVisiblePostGoods">
      <el-form :model="listPostGoods">
        <el-form-item>
          <h2>请输入需要添加的信息：</h2>
        </el-form-item>
        <el-form-item label="商品名字: " :label-width="formLabelWidth">
          <el-input v-model="listPostGoods.goodsName" autocomplete="off"></el-input>
        </el-form-item>
<!--        <el-form-item label="图片: " :label-width="formLabelWidth">-->
<!--          <el-input v-model="listPostGoods.goodsImg" autocomplete="off"></el-input>-->
<!--        </el-form-item>-->
        <el-form-item label="价格: " :label-width="formLabelWidth">
          <el-input v-model="listPostGoods.goodsPrice" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="描述: " :label-width="formLabelWidth">
          <el-input v-model="listPostGoods.goodsDescribe" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="对应的大类于小类: " :label-width="formLabelWidth">
          <template>
          <el-select v-model="listPostGoods.categoryId" @change="categoryListTypes(listPostGoods.categoryId)" placeholder="请选择大类">
            <el-option
              v-for="item in categoryGoodsAddList"
              :key="item.categoryId"
              :label="item.categoryName"
              :value="item.categoryId"
            >
            </el-option>
          </el-select>
        </template>
          <template>
            <el-select v-model="listPostGoods.goodsType" placeholder="请选择小类">
              <el-option
                v-for="item in typesGoodsAddList"
                :key="item.typeId"
                :label="item.typeName"
                :value="item.typeId">
              </el-option>
            </el-select>
          </template>
        </el-form-item>

        <el-form-item label="月销售: " :label-width="formLabelWidth">
          <el-input v-model="listPostGoods.monthlySales" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="总销售: " :label-width="formLabelWidth">
          <el-input v-model="listPostGoods.totalSales" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisiblePutGoods = false">取 消</el-button>
        <el-button type="primary" @click="dialogFormVisiblePostGoodsGo(listPostGoods)">确 定</el-button>
      </div>
    </el-dialog>

    <!--详细-->
    <el-dialog title="详细"  :visible.sync="dialogFormVisiblePostGoodsDetailed">
          <h3>详细信息如下：</h3>
          <p v-for="detailedList in detailedList">
            商品规格：{{detailedList.characterName}} &nbsp; 对应价格： {{detailedList.characterPrice}}
          </p>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisiblePostGoodsDetailed = false">取 消</el-button>
        <el-button type="primary" @click="dialogFormVisiblePostGoodsDetailed = false">确 定</el-button>
      </div>
    </el-dialog>

    <!--修改商品弹窗-->
    <el-dialog title="修改商品"  :visible.sync="dialogFormVisiblePutGoods">
      <el-form :model="listPutGoods">
        <el-form-item>
          <h2>请输入需要修改的信息：</h2>
        </el-form-item>
        <el-form-item label="商品名字: " :label-width="formLabelWidth">
          <el-input v-model="listPutGoods.goodsName" autocomplete="off"></el-input>
        </el-form-item>
<!--        <el-form-item label="图片: " :label-width="formLabelWidth">-->
<!--          <el-input v-model="listPutGoods.goodsImg" autocomplete="off"></el-input>-->
<!--        </el-form-item>-->
        <el-form-item label="价格: " :label-width="formLabelWidth">
          <el-input v-model="listPutGoods.goodsPrice" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="描述: " :label-width="formLabelWidth">
          <el-input v-model="listPutGoods.goodsDescribe" autocomplete="off"></el-input>
        </el-form-item>


<!--        <el-form-item label="对应的大类于小类: " :label-width="formLabelWidth">-->
<!--          <template>-->
<!--            <el-select v-model="listPutGoods.categoryId" @change="categoryListTypes(listPutGoods.categoryId)" placeholder="请选择大类">-->
<!--              <el-option-->
<!--                v-for="item in categoryGoodsAddList"-->
<!--                :key="item.categoryId"-->
<!--                :label="item.categoryName"-->
<!--                :value="item.categoryId"-->
<!--              >-->
<!--              </el-option>-->
<!--            </el-select>-->
<!--          </template>-->
<!--          <template>-->
<!--            <el-select v-model="listPutGoods.goodsType" placeholder="请选择小类">-->
<!--              <el-option-->
<!--                v-for="item in typesGoodsAddList"-->
<!--                :key="item.typeId"-->
<!--                :label="item.typeName"-->
<!--                :value="item.typeId">-->
<!--              </el-option>-->
<!--            </el-select>-->
<!--          </template>-->
<!--        </el-form-item>-->

        <el-form-item label="月销售: " :label-width="formLabelWidth">
          <el-input v-model="listPutGoods.monthlySales" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="总销售: " :label-width="formLabelWidth">
          <el-input v-model="listPutGoods.totalSales" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisiblePutGoods = false">取 消</el-button>
        <el-button type="primary" @click="dialogFormVisiblePutGoodsGo(listPutGoods)">确 定</el-button>
      </div>
    </el-dialog>

  </div>
  </div>
</template>

<script>
// import { getList } from '@/api/table'
import { goodsList,goodsDelApi,dialogFormVisiblePutGoodsApi,dialogFormVisiblePostGoodsApi,goodsStateztApi,detailedApi,categoryList,typesCategoryId,pagingList,searchApi } from '@/api/server-goods'


export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      detailedList:[{
        characterName:null,
      }],
      input: null, //搜索数据
      currentPage:null, //页数
      count: 18, //赋值意义，代码出差，分页不会有问题(好像也没啥用)
      list: null,
      listLoading: true,
      dialogFormVisiblePutGoods: false, // 修改商品是否弹窗
      dialogFormVisiblePostGoods: false, //添加商品是否弹窗
      dialogFormVisiblePostGoodsDetailed:false,//详细弹窗
      categoryGoodsAddList:null, //所有大类(添加商品)
      typesGoodsAddList:null, //对应的小类(添加商品)
      listPutGoods: [//修改商品的数据
        {
          "goodsId": null,
          "goodsName": null,
          "goodsImg": null,
          "goodsPrice": null,
          "goodsDescribe": null,
          "goodsType": null,
          "categoryId": null,
          "monthlySales": null,
          "totalSales": null,
          "goodsState": null,
          "goodsCharacters": null,
          "category": null,
          "types": null,
          "categoryList": null,
          "typesList": null
        },
      ],
      listPostGoods: [  //添加商品的数据
        {
          "goodsId": null,
          "goodsName": null,
          "goodsImg": null,
          "goodsPrice": null,
          "goodsDescribe": null,
          "goodsType": null,
          "categoryId": null,
          "monthlySales": null,
          "totalSales": null,
          "goodsState": null,
          "goodsCharacters": null,
          "category": null,
          "types": null,
          "categoryList": null,
          "typesList": null
        },
      ],
      formLabelWidth: '140px', //添加修改页面的长度

    }
  },
  created() {
    this.fetchData()
  },
  methods: {

    //搜索
    search(input){
      var that = this;
      that.currentPage = 1;
      searchApi(input,function (msg) {
        that.list = msg.data;
        that.count = msg.count;  //分页条数
      })

    },

    //手动刷新
    refresh(){
      var that = this;
      that.input = null;//搜索数据 清空 防止数据污染
      that.currentPage = 1;
      that.fetchData();

    },

    //分页
    handleCurrentChange(val) {
      var that = this;
      pagingList(val,that.input,function(msg) {
        that.list = msg.data;
        if (msg.msg === '查询成功！') {
          that.listLoading = false
        }
      })
    },

    //点击大类触发对应小类
    categoryListTypes(id){
      let that = this;
      that.typesGoodsAddList = null;
      typesCategoryId(id,function (msg) {
        console.log("msg");
        console.log(msg.data)
        that.typesGoodsAddList = msg.data;
      })
    },


    //弹出添加弹窗，并且获取所有大类
    dialogFormVisiblePostGoodsTC(){
      let that = this;
      that.dialogFormVisiblePostGoods = true;
      categoryList(function(msg){
        that.categoryGoodsAddList = msg.data;
        console.log("categoryGoodsAddList");
        console.log(that.categoryGoodsAddList);


      })

    },

    //详细
    detailed(detailedId){
      var that = this;
      that.dialogFormVisiblePostGoodsDetailed = true;
      detailedApi(detailedId,function(msg){
        that.detailedList = msg.data;
      })
    },


    goodsStatezt(index){
      var that = this;
      var ztId = that.list[index].goodsState;
      var ztIndex = that.list[index].goodsId;
      goodsStateztApi(ztId,ztIndex,function(msg){
        that.fetchData();
      })

      // var hao = that.list[index].goodsState;
      // if(hao == 1){
      //   hao = 0;
      // }else{
      //   hao =1;
      // }
      // that.list[index].goodsState = hao;

    },

    /*添加*/
    dialogFormVisiblePostGoodsGo(listPostGoods){
      var that = this;
      dialogFormVisiblePostGoodsApi(listPostGoods,function(msg){
        if(msg.msg == '添加成功'){
          that.dialogFormVisiblePostGoods = false;
          alert(msg.msg);
          that.fetchData();
        }
      })
    },


    /*根据id修改类型*/
    dialogFormVisiblePutGoodsGo(listPutGoods){
      var that = this;
      dialogFormVisiblePutGoodsApi(listPutGoods,function (msg) {
        if(msg.msg == '修改成功'){
          that.dialogFormVisiblePutGoods = false;
          alert(msg.msg);
        }
      })
    },

    /*修改获取当前数据*/
    dialogFormVisiblePutGoodsId(index){
      var that = this;
      categoryList(function(msg){
        that.categoryGoodsAddList = msg.data;
      })
      that.listPutGoods = that.list[index];
      //查看当前是否赋值成功
      console.log(that.listPutGoods);
      that.dialogFormVisiblePutGoods = true;
    },


    // 根据id删除类型
    goodsDel(goodsId) {
      var that = this;
      goodsDelApi(goodsId, function(msg) {
        alert(msg.data.msg);
        if(msg.data.msg == '删除成功'){
          that.fetchData()
        }

      })
    },


    fetchData() {
      var that = this
      that.listLoading = true
      goodsList(function(msg) {
        that.list = msg.data;
        that.count = msg.count;  //分页条数
        if (msg.msg === '查询成功！') {
          that.listLoading = false
        }
      })
    }
  }
}
</script>
